package com.example.nio.chat;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private int port;
    private ServerSocket serverSocket;

    public Server(int port) {
        this.port = port;
    }

    public void init() throws IOException {
        serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(port));
        Socket socket;
        System.out.println("server started on port " + port);
        while ((socket = serverSocket.accept()) != null) {
            System.out.println("Server accepted");
            try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
                String s = bufferedReader.readLine();
                System.out.println("get msg: " + s);
            } catch (IOException e) {

            }
        }
    }

    public static void main(String[] args) throws IOException {
        Server server = new Server(5000);
        server.init();
    }
}
